
package com.daatak.mvc.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.daatak.mvc.common.JsonResponse;
import com.daatak.user.business.UserBeanI;
import com.daatak.user.domain.UserAddress;
import com.daatak.user.domain.UserObject;

@Controller
public class UserController {
	
	@Autowired
	UserBeanI userBean;
	
	@InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true));
    }
	
	@RequestMapping(value="/user/home", method = RequestMethod.GET)
	public String getUserGrid(ModelMap model) {
		
		model.addAttribute("userList",userBean.list());
		return "home.user/userGrid";
	}
	@RequestMapping(value="/user", method = RequestMethod.GET)
	public String userGridByAjax(ModelMap model) {
		
		model.addAttribute("userList",userBean.list());
		return "ajax.user/userGrid";
	}
	
	@RequestMapping(value="/user/userGrid", method = RequestMethod.GET)
	public @ResponseBody List<UserObject> getUsers(ModelMap model) {
		
		return userBean.list();
	}
	
	@RequestMapping(value="/user/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		
		UserPojo pojo = new UserPojo();
		model.addAttribute("user",pojo);
		
		return "ajax.user/_userForm";
	}
	@RequestMapping(value="/user/details/{id}", method = RequestMethod.GET)
	public String userDetails(@PathVariable("id") Integer id,ModelMap model) {
		
		UserObject user = userBean.findById(id);
		
		model.addAttribute("user", user);
		
		return "ajax.user/userDetails";
	}
	@RequestMapping(value="/user/getInputScreen", method = RequestMethod.GET)
	public String getInputScreen(ModelMap model) {
		
		UserPojo pojo = new UserPojo();
		
		model.addAttribute("user",pojo);
		
		return "ajax.user/_userForm";
	}
	
	@RequestMapping(value="/user/getInputScreen/{id}", method = RequestMethod.GET)
	public String editUser(@PathVariable("id") Integer id, ModelMap model) {
		
		UserPojo pojo = new UserPojo();
		UserObject user = userBean.findById(id);
		pojo.setUser(user);
		
		
		Set<UserAddress> listAdd = user.getAddress();
		for (Iterator iterator = listAdd.iterator(); iterator.hasNext();) {
			UserAddress userAddress = (UserAddress) iterator.next();
			pojo.setAddress(userAddress);
			
		}
		
		model.addAttribute("user",pojo);
		
		return "ajax.user/_userForm";
	}
	
	@RequestMapping(value="/user/add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse add(@ModelAttribute UserPojo userPojo, BindingResult result, ModelMap model) {
		JsonResponse validation = new JsonResponse();
		UserValidator validator = new UserValidator();
		validator.validate(userPojo, result);
		if(result.hasErrors()){
			validation.setStatus("ERROR");
			validation.setFieldError(result.getFieldErrors());
			  
		}
		else
		{ 
			userPojo.getUser().getAddress().add(userPojo.getAddress());
			userBean.create(userPojo.getUser());
		}
		
		return validation;
	}

	public void setUserBean(UserBeanI userBean) {
		this.userBean = userBean;
	}
	
	


}
